package com.ysstech.system.controller;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.entity.Logger;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Slf4j
@Api(tags = "操作日志API")
@RequestMapping(value = "/logger")
public class LoggerController extends BaseController {

    /**
     * 跳转操作日志信息页面
     *
     * @return
     * @throws Exception
     */
    @YssPermissions(value = {"system:logger"})
    @ApiOperation(value = "跳转操作日志页面")
    @YssLog(module = "操作日志管理", remark = "跳转操作日志页面")
    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "system/logger";
    }


    @YssPermissions(value = {"system:logger"})
    @YssLog(module = "操作日志管理", remark = "查询操作日志")
    @ApiOperation(value = "查询操作日志")
    @RequestMapping(value = "/listLogger")
    @ResponseBody
    public RestResult listLogger(Logger logger, String page, String rows) throws Exception {
        return ResultGenerator.getSuccessResult("查询定时任务日志成功",
                loggerService.queryAllLogger(logger, this.getPage(page, rows)));
    }


    @YssPermissions(value = {"system:logger:del"})
    @YssLog(module = "操作日志管理", remark = "删除操作日志")
    @ApiOperation(value = "删除操作日志")
    @RequestMapping(value = "/del")
    @ResponseBody
    public RestResult del(String id) throws Exception {
        if (StringUtils.isBlank(id)) {
            QueryWrapper<Logger> wrapper = new QueryWrapper<>();
            loggerService.remove(wrapper);
            return ResultGenerator.getSuccessResult("删除定时任务日志成功");
        }
        List<String> logIds = JSONArray.parseArray(id, String.class);
        if (CollectionUtils.isEmpty(logIds)) {
            QueryWrapper<Logger> wrapper = new QueryWrapper<>();
            loggerService.remove(wrapper);
            return ResultGenerator.getSuccessResult("删除定时任务日志成功");
        }
        loggerService.removeByIds(logIds);
        return ResultGenerator.getSuccessResult("删除定时任务日志成功");
    }
}
